﻿#include "EditorPageAreaView.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Widgets/TextWidget.h"
#include "Widgets/ImageWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/Positioners/HorizontalPositionerWidget.h"
#include "Widgets/Positioners/FunctionPositionerWidget.h"
#include "Widgets/EditableTextWidget.h"
#include "Widgets/EditorEditableTextWidget.h"
#include "Widgets/EditorImageButton.h"

#include "Pages/EditorPageViewModel.h"
#include "Pages/EditorPageView.h"

#include "Window/EditorWindowGroupViewModel.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

void REditorPageAreaView::BindViewModel(TSharedObjectPtr<REditorPageAreaViewModel> InViewModel)
{
    ViewModel = InViewModel;

    auto CreatePageView=[this](TSharedObjectPtr<REditorPageViewModel> PageViewModel)
    {
        auto PageViewClass = REditor::Get()->GetViewClass(PageViewModel->GetClass());
        auto PageView = NewObject<REditorPageView>(PageViewClass,this);
        PageView->BindViewModel(PageViewModel);
        Pages.Add(PageView);
    };

    for(int64_t PageIndex=0;PageIndex<ViewModel->GetPageNum();PageIndex++)
    {
        auto PageViewModel = ViewModel->GetPage(PageIndex);
        CreatePageView(PageViewModel);
    };
    ViewModel->OnPageAdded.Bind([this,CreatePageView](TSharedObjectPtr<REditorPageViewModel> PageViewModel)
    {
        CreatePageView(PageViewModel);
        CreatePageTitle(Pages.Num()-1);
        CreatePageOperation(Pages.Num()-1);
        CreatePageWidget(Pages.Num()-1);
    });
    ViewModel->OnPageClosed.Bind([this](int64_t Index)
    {
        Pages.RemoveAt(Index);
        if(PageTitleBar)
        {
            //remove title and grey line at right of the title
            PageTitleBar->RemoveSubWidgetAt(PageTitleBar->GetSubWidgetNum()-1);
            PageTitleBar->RemoveSubWidgetAt(PageTitleBar->GetSubWidgetNum()-1);
        }

        if(PageOperationsParent)
        {
            //remove page operation buttons at right of the title
            PageOperationsParent->RemoveSubWidgetAt(PageOperationsParent->GetSubWidgetNum()-1);
        }

        if(PageContent)
        {
            //remove  all page content widget at right of the title
            while(Index<PageContent->GetSubWidgetNum())
            {
                PageContent->RemoveSubWidgetAt(PageContent->GetSubWidgetNum()-1);
            }

            //regenerate the page content widget
            for(int64_t i=Index;i<ViewModel->GetPageNum();i++)
            {
                CreatePageWidget(i);
            }
        }

    });
    ViewModel->OnPageReplaced.Bind([this](int64_t PageIndex)
    {
        auto PageViewClass = REditor::Get()->GetViewClass(ViewModel->GetPage(PageIndex)->GetClass());
        Pages[PageIndex] = NewObject<REditorPageView>(PageViewClass,this);
        Pages[PageIndex] ->BindViewModel(ViewModel->GetPage(PageIndex));

        //CreatePageTitle(PageIndex);
        if(PageContent)
        {
            auto PageContentParent=PageContent->GetSubWidget(PageIndex);
            PageContentParent->ClearSubWidgets();
            Pages[PageIndex]->CreateWidget(PageContentParent);
        }

        if(PageOperationsParent)
        {
            auto PageOperationParent=PageOperationsParent->GetSubWidget(PageIndex);
            PageOperationParent->ClearSubWidgets();
            CreatePageOperation(PageIndex);
        }
    });


    // for(int64_t SubAreaIndex=0;SubAreaIndex<ViewModel->GetSubAreaNum();SubAreaIndex++)
    // {
    //     auto SubAreaViewModel = ViewModel->GetSubArea(SubAreaIndex);
    //     auto SubAreaView = NewObject<REditorPageAreaView>(this);
    //     SubAreaView->BindViewModel(SubAreaViewModel);
    //     SubAreaViews.Add(SubAreaView);
    // }
    
    // ViewModel->OnLayoutChanged.Bind([this,CreatePageView]()
    // {
    //     Pages.Clear();
    //     SubAreaViews.Clear();

    //     for(int64_t SubAreaIndex=0;SubAreaIndex<ViewModel->GetSubAreaNum();SubAreaIndex++)
    //     {
    //         auto SubAreaViewModel = ViewModel->GetSubArea(SubAreaIndex);
    //         auto SubAreaView = NewObject<REditorPageAreaView>(this);
    //         SubAreaView->BindViewModel(SubAreaViewModel);
    //         SubAreaViews.Add(SubAreaView);
    //     }

    //     for(int64_t PageIndex=0;PageIndex<ViewModel->GetPageNum();PageIndex++)
    //     {
    //         auto PageViewModel = ViewModel->GetPage(PageIndex);
    //         CreatePageView(PageViewModel);
    //     }

    //     if(RootWidget.IsValid())
    //     {
    //         RootWidget->ClearSubWidgets();
    //         CreateWidgetInRoot();
    //     }
    // });


}

void REditorPageAreaView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    RootWidget=NewWidget<RWidget>(InParent);
    RootWidget->Anchor.Fill(InParent);

    CreateWidgetInRoot();

}

void REditorPageAreaView::CreateWidgetInRoot()
{
    assert(RootWidget.IsValid());

    // if(ViewModel->GetSubAreaNum()>0)
    // {
    //     CreateSubAreas();
    // }
    // else
    //{
        CreatePages();
    //}
    
    CreateFindAndReplaceWidget();
}

void REditorPageAreaView::CreatePages()
{
    auto TitleArea= NewWidget<RWidget>(RootWidget);
    TitleArea->Anchor.LeftReference=RootWidget;
    TitleArea->Anchor.RightReference=RootWidget;
    TitleArea->Anchor.TopReference=RootWidget;
    TitleArea->Height= ViewModel->TitleBarHeight;    

    //标题栏的背景色
    auto Background=NewWidget<RRectangleWidget>(TitleArea);
    Background->FillColor=REditor::Get()->CurrentTheme->BackgroundColor;
    Background->Anchor.Fill(TitleArea);

    auto TitleBar= NewWidget<RHorizontalPositionerWidget>(TitleArea);
    TitleBar->Anchor.VerticalFill(TitleArea);
    TitleBar->Anchor.LeftReference=RootWidget;
    TitleBar->Anchor.RightReference=RootWidget;
    TitleBar->Anchor.RightOffset=[this]()->float{
        return ViewModel->GetTitleOperationBarWidth() +ViewModel->TitleOperationBarRightPadding;
    };
    TitleBar->Spacing=0;
    this->PageTitleBar=TitleBar;

    auto OperationBar= NewWidget<RHorizontalPositionerWidget>(TitleArea);
    OperationBar->Anchor.VerticalFill(TitleBar);
    OperationBar->Anchor.RightReference=RootWidget;
    OperationBar->Anchor.RightOffset= ViewModel->TitleOperationBarRightPadding;
    OperationBar->Width= [this]()->float{
        return ViewModel->GetTitleOperationBarWidth(); 
    };
    OperationBar->Spacing=ViewModel->TitleOperationBarSpaceing;

    PageOperationsParent= NewWidget<RWidget>(OperationBar);
    PageOperationsParent->Anchor.VerticalFill(OperationBar);
    PageOperationsParent->Width=[this]()->float{
        return ViewModel->GetPageOperationBarWidth();
    };

    //add page's operation buttons
    for(int64_t i=0;i<ViewModel->GetPageNum();i++)
    {
        CreatePageOperation(i);
    }

    //add split operation
    const float ButtonSize=30;
    const float ImageSize=26;
    {
        auto AreaSplitButton=NewWidget<RMouseWidget>(OperationBar);
        AreaSplitButton->Anchor.VerticalCenterReference=OperationBar;
        AreaSplitButton->Width=ButtonSize;
        AreaSplitButton->Height=ButtonSize;
        AreaSplitButton->OnClicked.BindLambda([this](const SMouseButtonEvent&){
            ViewModel->SplitArea();
            return true;
        });
        RMouseWidgetWeakPtr AreaSplitButtonWeak=AreaSplitButton;

        auto Background=NewWidget<RRectangleWidget>(OperationBar);
        Background->Anchor.Fill(AreaSplitButton);
        Background->Radius= REditor::Get()->CurrentTheme->ImageButtonRadius;
        Background->FillColor=
            REditor::Get()->CurrentTheme->ImageButtonHovorColor;
        Background->Visible=[AreaSplitButtonWeak]()->bool{
            return AreaSplitButtonWeak->IsHovered();
        };

        auto Icon=NewWidget<RImageWidget>(OperationBar);
        Icon->Anchor.CenterIn(AreaSplitButton);
        Icon->Width=ImageSize;
        Icon->Height=ImageSize;
        Icon->Image=[this](){
            return ViewModel->GetSplitAreaButtonIcon();
        };

        //TODO add tips for the button
        //向右拆分编辑器\n[Alt]向下拆分编辑器

    }


    for(int64_t i=0;i<ViewModel->GetPageNum();i++)
    {
        CreatePageTitle(i);
    }

    auto PageContentShared= NewWidget<RRectangleWidget>(RootWidget);
    PageContentShared->Anchor.LeftReference=RootWidget;
    PageContentShared->Anchor.RightReference=RootWidget;
    PageContentShared->Anchor.TopReference=TitleBar;
    PageContentShared->Anchor.TopReferenceEdge=EVerticalWidgetEdge::Bottom;
    PageContentShared->Anchor.BottomReference=RootWidget;
    PageContentShared->FillColor=ViewModel->PageAreaBackgroundColor;
    this->PageContent=PageContentShared;

    for(int64_t i=0;i<ViewModel->GetPageNum();i++)
    {
        CreatePageWidget(i);
    }

    //show a grey background when no page is shown
    auto GreyBackground=NewWidget<RRectangleWidget>(RootWidget);
    GreyBackground->Anchor.Fill(RootWidget);
    GreyBackground->Visible=[this]()->bool{
        return ViewModel->GetPageNum()==0;
    };
    GreyBackground->FillColor=ViewModel->PageAreaBackgroundColor;

    auto EmptyText=NewWidget<RTextWidget>(GreyBackground);
    EmptyText->Anchor.Fill(GreyBackground);
    EmptyText->Text=U"未打开任何页面";
    EmptyText->VerticalAlignment=ETextVerticalAlignment::Center;
    EmptyText->HorizontalAlignment=ETextHorizontalAlignment::Center;

}

void REditorPageAreaView::CreatePageTitle(int64_t PageIndex)
{
    auto TitleBar =  NewWidget<RWidget>(PageTitleBar);
    TitleBar->Height = ViewModel->TitleBarHeight;

    RWidgetWeakPtr TitleBarWeak=TitleBar;

    auto Mouse=NewWidget<RMouseWidget>(TitleBar);
    Mouse->Anchor.Fill(TitleBar);
    Mouse->OnClicked.BindLambda([this,PageIndex](const SMouseButtonEvent& Mouse){
        if(Mouse.GetButton()==EMouseButton::Right)
        {
            ShowPageTitleContextMenu(PageIndex);
        }
        else
        {
            ViewModel->SelectPage(PageIndex);
        }
        return true;
    });
    RMouseWidgetWeakPtr MouseWeak=Mouse;
    
    //a gey line at bottom of the title bar
    //will be covered is this page content is shown
    auto BottomLine=NewWidget<RRectangleWidget>(TitleBar);
    BottomLine->FillColor=SColor::FromHexRGB(0x2B2B2B);
    BottomLine->Height=1;
    BottomLine->Anchor.HorizontalFill(TitleBar);
    BottomLine->Anchor.BottomReference=TitleBar;

    //a gey background on page content shown or title bar is hovered
    auto GreyBackground=NewWidget<RRectangleWidget>(TitleBar);
    GreyBackground->FillColor=ViewModel->PageAreaBackgroundColor;
    GreyBackground->Anchor.Fill(TitleBar);
    GreyBackground->Visible=[this,PageIndex,MouseWeak]()->bool{
       return 
        MouseWeak->IsHovered()||
        ViewModel->IsShowingPageContent(PageIndex);
    };

    //a highlight line at top of the title bar
    auto HighlightLine=NewWidget<RRectangleWidget>(TitleBar);
    HighlightLine->FillColor = SColor::FromHexRGB(0x0078D4);
    HighlightLine->Height = 1;
    HighlightLine->Anchor.HorizontalFill(TitleBar);
    HighlightLine->Visible=[this,PageIndex]()->bool{
        return ViewModel->IsHighlighted(PageIndex);
    };
    /*
    *    ----------------------------------------------
    *    |                                            |
    *    |<-->Icon<-->TitleTextString<-->CloseIcon<-->|
    *    |  A       B                  C            D |
    *    |                                            |
    *    ---------------------------------------------
    *   there is 3 parts in the title bar:
    *   a icon on the left
    *   a title text in the middle
    *   a close icon on the right
    *   so there is 4 blank spaces between the 3 parts, we call them A,B,C,D
    */
    static const float A=8;
    static const float B=4;
    static const float C=4;
    static const float D=8;
    static const float IconSize=20;
    static const float CloseIconSize=20;

    auto Icon=NewWidget<RImageWidget>(TitleBar);
    Icon->Height=IconSize;
    Icon->Anchor.VerticalCenterReference=TitleBar;
    Icon->Width=IconSize;
    Icon->X=A;
    Icon->Image=[this,PageIndex]()->TSharedObjectPtr<RTexture2D>{
        return ViewModel->GetPageIcon(PageIndex);
    };


    auto CloseIconBackground=NewWidget<RRectangleWidget>(TitleBar);
    CloseIconBackground->FillColor=SColor::FromHexRGB(0x323232);
    CloseIconBackground->Width=CloseIconSize;
    CloseIconBackground->Anchor.VerticalCenterReference=TitleBar;
    CloseIconBackground->Height=CloseIconSize;
    CloseIconBackground->Anchor.RightReference=TitleBar;
    CloseIconBackground->Anchor.RightOffset=D;
    CloseIconBackground->Radius=4;




    auto CloseIconImage=NewWidget<RImageWidget>(TitleBar);
    CloseIconImage->Anchor.Fill(CloseIconBackground);
    CloseIconImage->Width=CloseIconSize;
    CloseIconImage->Anchor.VerticalCenterReference=TitleBar;
    CloseIconImage->Height=CloseIconSize;
    CloseIconImage->Anchor.RightReference=TitleBar;
    CloseIconImage->Anchor.RightOffset=D;

    auto CloseIconMouse=NewWidget<RMouseWidget>(TitleBar);
    CloseIconMouse->Anchor.Fill(CloseIconBackground);
    CloseIconMouse->OnClicked.BindLambda([this,PageIndex](const SMouseButtonEvent&){
        ViewModel->ClosePage(PageIndex);
        return true;
    });



    RMouseWidgetWeakPtr CloseIconMouseWeak=CloseIconMouse;
    CloseIconBackground->Visible=[CloseIconMouseWeak,TitleBarWeak]()->bool{
        return CloseIconMouseWeak->IsHovered();
    };

    CloseIconImage->Image=[this,PageIndex,CloseIconMouseWeak,MouseWeak]()->TSharedObjectPtr<RTexture2D>{
        return ViewModel->GetCloseIcon(
            PageIndex
            ,MouseWeak->IsHovered()
            ,CloseIconMouseWeak->IsHovered()
            );
    };



    auto Text = NewWidget<RTextWidget>(TitleBar);
    Text->Text = [this,PageIndex]()->CString{
        return ViewModel->GetPageTitle(PageIndex);
    };
    Text->Anchor.LeftReference=Icon;
    Text->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    Text->Anchor.LeftOffset=B;
    Text->Anchor.RightReference=CloseIconBackground;
    Text->Anchor.RightReferenceEdge=EHorizontalWidgetEdge::Left;
    Text->Anchor.RightOffset=C;
    Text->Anchor.VerticalFill(TitleBar);
    Text->FontSize=14;
    Text->VerticalAlignment=ETextVerticalAlignment::Center;

    RTextWidgetWeakPtr TextWeak=Text;
    TitleBar->Width=[ TextWeak]()->float{
        const float MinTextWidth=100;
        float TextWidth=TextWeak->GetTextSize().X;
        if(TextWidth<MinTextWidth)
        {
            TextWidth=MinTextWidth;
        }
        return A+B+C+D+IconSize+CloseIconSize+ TextWidth;
    };


    //add a grey line at right of the title bar
    auto Line=NewWidget<RRectangleWidget>(PageTitleBar);
    Line->FillColor=SColor::FromHexRGB(0x2B2B2B);
    Line->Width=1;
    Line->Anchor.VerticalFill(TitleBar);    
}

void REditorPageAreaView::CreatePageOperation(int64_t Index)
{
    auto SinglePageOperationParent=NewWidget<RWidget>(PageOperationsParent);
    SinglePageOperationParent->Anchor.Fill(PageOperationsParent);
    SinglePageOperationParent->Visible=[this,Index]()->bool{
        return ViewModel->IsShowingPageOperation(Index);
    };
    auto Page=Pages[Index];
    Page->FillOperations(SinglePageOperationParent);
}

void REditorPageAreaView::CreatePageWidget(int64_t Index)
{
    if(!PageContent.IsValid())
    {
        return;
    }

    TSharedObjectPtr<REditorPageView> Page= Pages[Index];
    
    auto PageContentParent=NewWidget<RWidget>(PageContent);
    PageContentParent->bClipSubWidgets=true;
    PageContentParent->Anchor.Fill(PageContent);
    PageContentParent->Visible=[this,Index]()->bool{
        return ViewModel->IsShowingPageContent(Index);
    };
    Page->CreateWidget(PageContentParent);
}

void REditorPageAreaView::CreateFindAndReplaceWidget()
{
    auto FindAndReplaceWidget=NewWidget<RRectangleWidget>(RootWidget);
    FindAndReplaceWidget->Anchor.RightReference=RootWidget;
    FindAndReplaceWidget->Anchor.RightOffset=30;
    FindAndReplaceWidget->Width=[this]()->float{
        return ViewModel->GetFindAndReplaceWidgetWidth();
    };
    FindAndReplaceWidget->Visible=[this]()->bool{
        return ViewModel->IsFindOrReplaceVisible();
    };

    FindAndReplaceWidget->Anchor.TopReference=RootWidget;
    FindAndReplaceWidget->Anchor.TopOffset= 60.f;
    FindAndReplaceWidget->Height=[this]()->float{
        return ViewModel->IsReplace()? ViewModel->LineHeight*2  :ViewModel->LineHeight;
    };

    FindAndReplaceWidget->FillColor=REditor::Get()->CurrentTheme->BackgroundColor;
    FindAndReplaceWidget->BorderSize=2;
    FindAndReplaceWidget->BorderColor=REditor::Get()->CurrentTheme->SpliterColor;

    auto DragArea=NewWidget<RMouseWidget>(FindAndReplaceWidget);
    DragArea->Anchor.VerticalFill(FindAndReplaceWidget);
    DragArea->Width= ViewModel->DragBarWidth;
    RMouseWidgetWeakPtr DragAreaWeak=DragArea;
    auto DragAreaBackground=NewWidget<RRectangleWidget>(FindAndReplaceWidget);
    DragAreaBackground->Anchor.Fill(DragArea);
    DragAreaBackground->FillColor=[DragAreaWeak]()->SColor{
        return DragAreaWeak->IsHovered()?
            SColor::FromHexRGB(0x0078D4)
            :SColor::FromHexRGB(0x2B2B2B);
    };

    //切换是否显示替换控件的按钮
    auto ToggleReplaceButton=NewWidget<RMouseWidget>(FindAndReplaceWidget);
    ToggleReplaceButton->Anchor.LeftReference=DragArea;
    ToggleReplaceButton->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    ToggleReplaceButton->Width= ViewModel->ToggleReplaceButtonWidth;
    ToggleReplaceButton->Anchor.VerticalFill(FindAndReplaceWidget);
    ToggleReplaceButton->OnClicked.BindLambda([this](const SMouseButtonEvent&){
        ViewModel->ToggleReplace();
        return true;
    });

    auto ToggleImageWidget=NewWidget<RImageWidget>(ToggleReplaceButton);
    ToggleImageWidget->Anchor.CenterIn(ToggleReplaceButton);
    ToggleImageWidget->Width=16;
    ToggleImageWidget->Height=16;
    ToggleImageWidget->Image=[this]()->TSharedObjectPtr<RTexture2D>{
        return ViewModel->GetToggleReplaceIcon();
    };

    auto InputTexts= NewWidget<RWidget>(FindAndReplaceWidget);
    InputTexts->Anchor.VerticalFill(FindAndReplaceWidget);
    InputTexts->Anchor.LeftReference=ToggleReplaceButton;
    InputTexts->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    InputTexts->Width=[this]()->float{
        return ViewModel->GetTextWidth();
    };

    auto FindLine=NewWidget<RWidget>(FindAndReplaceWidget);
    FindLine->Height=ViewModel->LineHeight;
    auto ReplaceLine=NewWidget<RWidget>(FindAndReplaceWidget);
    ReplaceLine->Height=ViewModel->LineHeight;
    ReplaceLine->Y=ViewModel->LineHeight;

    auto FindTextWidget=WHE::CreateEditableTextWidget(InputTexts);
    FindTextWidget.Widget->Anchor.HorizontalFill(InputTexts);
    FindTextWidget.Widget->Anchor.VerticalCenterReference=FindLine;
    FindTextWidget.Widget->Height= ViewModel->TextEditHeight;
    FindTextWidget.TextWidget->UpdateTextOnAccepted=true;
    FindTextWidget.TextWidget->UpdateTextOnLostFocus=true;
    FindTextWidget.TextWidget->OnTextUpdated.BindLambda([this](const CString& NewText){
        ViewModel->UpdateSearchText(NewText);
    });

    auto ReplaceWidget=NewWidget<RWidget>(FindAndReplaceWidget);
    ReplaceWidget->Visible=[this]()->bool{
        return ViewModel->IsReplace();
    };

    auto ReplaceTextWidget=WHE::CreateEditableTextWidget(ReplaceWidget);
    ReplaceTextWidget.Widget->Anchor.HorizontalFill(InputTexts);
    ReplaceTextWidget.Widget->Anchor.VerticalCenterReference=ReplaceLine;
    ReplaceTextWidget.Widget->Height=ViewModel->TextEditHeight;
    ReplaceTextWidget.TextWidget->Text=[this]()->CString{
        return ViewModel->GetReplaceText();
    };
    ReplaceTextWidget.TextWidget->OnTextUpdated.BindLambda([this](const CString& NewText){
        ViewModel->UpdateReplaceText(NewText);
    });

    auto FindResultText=NewWidget<RTextWidget>(FindAndReplaceWidget);
    FindResultText->Text=[this]()->CString{
        return ViewModel->GetFindResultText();
    };
    FindResultText->Width=[this]()->float{
        return ViewModel->GetFindResultTextWidth();
    };
    FindResultText->Visible=[this]()->bool{
        return ViewModel->IsShowFindResultText();
    };
    FindResultText->Anchor.LeftReference=InputTexts;
    FindResultText->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    FindResultText->Anchor.LeftOffset=ViewModel->InputTextRightPadding;
    FindResultText->Anchor.VerticalCenterReference=FindLine;
    FindResultText->Height= ViewModel->TextEditHeight;
    FindResultText->VerticalAlignment=ETextVerticalAlignment::Center;
    FindResultText->Color=[this]()
    {
        return ViewModel->GetFindResultTextColor();
    };

    auto FindButtonGroup=NewWidget<RHorizontalPositionerWidget>(FindAndReplaceWidget);
    FindButtonGroup->Anchor.RightReference=FindAndReplaceWidget;
    FindButtonGroup->Anchor.RightOffset=ViewModel->ButtonGroupRightPadding;
    FindButtonGroup->Width=ViewModel->FindButtonGroupWidth;
    FindButtonGroup->Spacing=ViewModel->ButtonsPadding;
    FindButtonGroup->Anchor.VerticalCenterReference=FindLine;
    FindButtonGroup->Height=ViewModel->ButtonsSize;

    auto ReplaceButtonGroup=NewWidget<RHorizontalPositionerWidget>(ReplaceWidget);
    ReplaceButtonGroup->Anchor.RightReference=FindAndReplaceWidget;
    ReplaceButtonGroup->Anchor.LeftReference=InputTexts;
    ReplaceButtonGroup->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    ReplaceButtonGroup->Anchor.LeftOffset=ViewModel->InputTextRightPadding;
    ReplaceButtonGroup->Spacing=ViewModel->ButtonsPadding;
    ReplaceButtonGroup->Anchor.VerticalCenterReference=ReplaceLine;
    ReplaceButtonGroup->Height=ViewModel->ButtonsSize;


    {
        WHE::SImageButtonCreateInfo CreateInfo;
        CreateInfo.Icon=[this]()->TSharedObjectPtr<RTexture2D>{
            return ViewModel->GetPreviousIcon();
        };
        CreateInfo.ToolTip=CString(U"上一个");
        CreateInfo.OnClick=[this](){
            ViewModel->GotoPreviousMatched();
        };
        CreateInfo.ParentWidget=FindButtonGroup;
        CreateInfo.Size=ViewModel->ButtonsSize;
        CreateInfo.IconSize=ViewModel->ButtonImageSize;
        CreateInfo.Enabled=[this]()->bool{
            return !ViewModel->IsFindResultEmpty();
        };
        WHE::CreateImageButton(CreateInfo);
    }

    {
        WHE::SImageButtonCreateInfo CreateInfo;
        CreateInfo.Icon=[this]()->TSharedObjectPtr<RTexture2D>{
            return ViewModel->GetNextIcon();
        };

        CreateInfo.ToolTip=CString(U"下一个");
        CreateInfo.OnClick=[this](){
            ViewModel->GotoNextMatched();
        };
        CreateInfo.ParentWidget=FindButtonGroup;
        CreateInfo.Size=ViewModel->ButtonsSize;
        CreateInfo.IconSize=ViewModel->ButtonImageSize;
        CreateInfo.Enabled=[this]()->bool{
            return !ViewModel->IsFindResultEmpty();
        };

        WHE::CreateImageButton(CreateInfo);
    }

    {
        WHE::CreateImageButton(
            ViewModel->FindInSelectionIcon
            ,CString(U"仅在选定内容中查找(未实现)")
            ,[this](){
                //TODO 仅在选定内容中查找
            }
            ,FindButtonGroup
            ,ViewModel->ButtonsSize
            ,ViewModel->ButtonImageSize
        );
    }

    {
        WHE::CreateImageButton(
            ViewModel->CloseFindAndReplaceIcon
            ,CString(U"关闭")
            ,[this](){
                ViewModel->CloseFindAndReplace();
            }
            ,FindButtonGroup
            ,ViewModel->ButtonsSize
            ,ViewModel->ButtonImageSize
        );
    }

    {
        WHE::CreateImageButton(
            ViewModel->ReplaceIcon
            ,CString(U"替换")
            ,[this](){
                ViewModel->Replace();
            }
            ,ReplaceButtonGroup
            ,ViewModel->ButtonsSize
            ,ViewModel->ButtonImageSize
        );
    }

    {
        WHE::CreateImageButton(
            ViewModel->ReplaceAllIcon
            ,CString(U"全部替换")
            ,[this](){
                ViewModel->ReplaceAll();
            }
            ,ReplaceButtonGroup
            ,ViewModel->ButtonsSize
            ,ViewModel->ButtonImageSize
        );
    }




}

// void REditorPageAreaView::CreateSubAreas()
// {
//     auto SubAreasParent=NewWidget<RFunctionPositionerWidget>(RootWidget);
//     SubAreasParent->Anchor.Fill(RootWidget);


//     TVector<TSharedObjectPtr<RWidget>> SubAreaWidgets;
//     TVector<TSharedObjectPtr<RWidget>> SplitterWidgets;

//     for(int64_t SubAreaIndex=0;SubAreaIndex< ViewModel->GetSubAreaNum();SubAreaIndex++)
//     {
//         if(SubAreaIndex>0)
//         {
//             auto Splitter=NewWidget<RRectangleWidget>(SubAreasParent);
//             Splitter->FillColor=SColor::FromHexRGB(0x2B2B2B);
//             SplitterWidgets.Add(Splitter);
//         }

//         auto SubAreaWidget=NewWidget<RWidget>(SubAreasParent);
//         SubAreaWidgets.Add(SubAreaWidget);

//         auto SubAreaView=SubAreaViews[SubAreaIndex];
//         SubAreaView->CreateWidget(SubAreaWidget);

//     } 

//     //add test widget
//     // auto TestWidget1=NewWidget<RRectangleWidget>(RootWidget);
//     // TestWidget1->FillColor=SColor::Red;
//     // TestWidget1->Anchor.Fill(SubAreaWidgets[0]);

//     // auto TestWidget2=NewWidget<RRectangleWidget>(RootWidget);
//     // TestWidget2->FillColor=SColor::Green;
//     // TestWidget2->Anchor.Fill(SubAreaWidgets[1]);


//     RFunctionPositionerWidgetWeakPtr SubAreasParentWeak=SubAreasParent;
//     auto ArrangeFunction=[this,SubAreasParentWeak](){
//         auto Size=SubAreasParentWeak->PresentSize;
//         auto Position=SubAreasParentWeak->PresentAbsPosition;
        

//         bool IsVertical=ViewModel->IsSubAreaVertical();
//         if(IsVertical)
//         {
//             float AvalibleContentHeight=Size.Y-
//                 REditor::Get()->CurrentTheme->SpliterSize * (ViewModel->GetSubAreaNum()-1);

//             //set the size of each sub area
//             float CurrentY=Position.Y;
//             for(int64_t Index=0;Index<ViewModel->GetSubAreaNum();Index++)
//             {
//                 if(Index>0)
//                 {
//                     auto Splitter=SubAreasParentWeak->GetSubWidget(Index*2-1);
//                     Splitter->PresentAbsPosition.X=Position.X;
//                     Splitter->PresentAbsPosition.Y=CurrentY;
//                     float SpliterHeight=REditor::Get()->CurrentTheme->SpliterSize;
//                     Splitter->PresentSize.X=Size.X;
//                     Splitter->PresentSize.Y=SpliterHeight;
//                     CurrentY+=SpliterHeight;
//                 }

//                 auto SubAreaWidget=SubAreasParentWeak->GetSubWidget(Index*2);
//                 SubAreaWidget->PresentAbsPosition.X=Position.X;
//                 SubAreaWidget->PresentAbsPosition.Y=CurrentY;
//                 float SubAreaHeight=ViewModel->GetSubAreaSizeWeight(Index)*AvalibleContentHeight;
//                 SubAreaWidget->PresentSize.X=Size.X;
//                 SubAreaWidget->PresentSize.Y=SubAreaHeight;
//                 CurrentY+=SubAreaHeight;
//             }

//         }
//         else
//         {
//             float AvalibleContentWidth=Size.X-
//                 REditor::Get()->CurrentTheme->SpliterSize * (ViewModel->GetSubAreaNum()-1);

//             //set the size of each sub area
//             float CurrentX=Position.X;
//             for(int64_t Index=0;Index<ViewModel->GetSubAreaNum();Index++)
//             {
//                 if(Index>0)
//                 {
//                     auto Splitter=SubAreasParentWeak->GetSubWidget(Index*2-1);
//                     Splitter->PresentAbsPosition.X=CurrentX;
//                     Splitter->PresentAbsPosition.Y=Position.Y;
//                     float SpliterWidth=REditor::Get()->CurrentTheme->SpliterSize;
//                     Splitter->PresentSize.X=SpliterWidth;
//                     Splitter->PresentSize.Y=Size.Y;
//                     CurrentX+=SpliterWidth;
//                 }

//                 auto SubAreaWidget=SubAreasParentWeak->GetSubWidget(Index*2);
//                 SubAreaWidget->PresentAbsPosition.X=CurrentX;
//                 SubAreaWidget->PresentAbsPosition.Y=Position.Y;
//                 float SubAreaWidth=ViewModel->GetSubAreaSizeWeight(Index)*AvalibleContentWidth;
//                 SubAreaWidget->PresentSize.X=SubAreaWidth;
//                 SubAreaWidget->PresentSize.Y=Size.Y;
//                 CurrentX+=SubAreaWidth;

//             }
//         }


//     };

//     SubAreasParent->ArrangeChildrenFunction.Bind(ArrangeFunction);
// }

void REditorPageAreaView::ShowPageTitleContextMenu(int64_t Index)
{
    REditorWindowGroupViewModel::Get()->ShowContextMenu(
        ViewModel->CreateTitleContext(Index)
        ,SMouseButtonEvent::GetMosuePositionOnScreen()
        ,350
    );
}
